	package pandora.data.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import pandora.connection.ConnectionManager;
import pandora.data.*;
import pandora.dto.AccountDTO;

public class JDBCAccountDAO implements IAccountDAO{

	private Connection conn;
	
	public JDBCAccountDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}
	
    protected void finalize() {
        ConnectionManager.getInstance().checkIn(conn);
    }
    
	public void delete(Connection conn, String accountOID) {
		// TODO Auto-generated method stub
		
	}

	public void insert(Connection conn, String accountOID, String login) {
		// TODO Auto-generated method stub
		
	}

	public boolean userValid(String usr, String psw) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		Boolean isValid = false;
		
		String sql = "SELECT A.* FROM Accounts A WHERE (A.user = ?) AND (A.pass = ?)";

        try {
        	stmt = conn.prepareStatement(sql);
        	
            stmt.setString(1, usr);
            stmt.setString(2, psw);
            
            result = stmt.executeQuery();

            if (result.next()) {
            	isValid = true;
            }           
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		return isValid;
	}

	public String selectAccountOIDByUser(String user){
		PreparedStatement stmt = null;
		ResultSet result = null;
		String usrOID = "";
		
		String sql = "SELECT A.* FROM Accounts A WHERE (A.user = ?) ";

        try {
        	stmt = conn.prepareStatement(sql);
        	
            stmt.setString(1, user);
            result = stmt.executeQuery();

            if (result.next()) {
            	usrOID = result.getString("OID");
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		return usrOID;
	}

	@Override
	public AccountDTO selectAccountByOID(String oid) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		AccountDTO ac = new AccountDTO();
		
		String sql = "SELECT A.* FROM Accounts A WHERE (A.OID = ?) ";

        try {
        	stmt = conn.prepareStatement(sql);
        	
            stmt.setString(1, oid);
            result = stmt.executeQuery();

            if (result.next()) {
            	ac.setUserName(result.getString("user"));
            	ac.setEmail(result.getString("email"));
            	ac.setPass(result.getString("pass"));
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		return ac;
	}
}
